//
//  WYBannerView.m
//  AdvertTest
//
//  Created by weiyuan on 2017/11/22.
//  Copyright © 2017年 weiyuan. All rights reserved.
//

#import "WYBannerView.h"
#import "WYBannerModel.h"

#define WY_Banner_Width CGRectGetWidth(self.bounds)
#define WY_Banner_Height CGRectGetHeight(self.bounds)
static const CGFloat repeateInterval = 2.0;

@interface WYBannerView ()
<
UIScrollViewDelegate
>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation WYBannerView

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

#pragma mark - base init
- (void)creatBaseViewWithModels:(NSArray<WYBannerModel *> *)dataModels
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake(WY_Banner_Width * (self.dataModels.count + 1), WY_Banner_Height);
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    [self addSubview:scrollView];
    self.scrollView = scrollView;
    
    for (int i = 0; i < dataModels.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(WY_Banner_Width * i, 0, WY_Banner_Width, WY_Banner_Height)];
        [self.scrollView addSubview:imageView];
        if (i == 0) {
            imageView.backgroundColor = [UIColor redColor];
        }else if (i == 1){
            imageView.backgroundColor = [UIColor greenColor];
        }else if (i == 2){
            imageView.backgroundColor = [UIColor yellowColor];
        }else{
            imageView.backgroundColor = [UIColor blueColor];
        }
        if (i == dataModels.count - 1) {
            UIImageView *imageViewReplace = [[UIImageView alloc]initWithFrame:CGRectMake(WY_Banner_Width * dataModels.count, 0, WY_Banner_Width, WY_Banner_Height)];
            imageViewReplace.backgroundColor = [UIColor redColor];
            [self.scrollView addSubview:imageViewReplace];
        }
    }
    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, self.dataModels.count * 10, 10)];
    pageControl.center = CGPointMake(WY_Banner_Width / 2, WY_Banner_Height - 10);
    pageControl.currentPage = 0;
    pageControl.numberOfPages = dataModels.count;
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    pageControl.hidesForSinglePage = YES;
    [self addSubview:pageControl];
    self.pageControl = pageControl;
    
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerPressed:)]];
}


#pragma mark - get/set
- (void)setDataModels:(NSArray<WYBannerModel *> *)dataModels
{
    _dataModels = dataModels;
    
    if (dataModels.count == 0) return;
    self.scrollView = nil;
    self.pageControl = nil;
    [self creatBaseViewWithModels:dataModels];
    
}

- (NSTimer *)timer
{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:repeateInterval target:self selector:@selector(bannerAutoScroll) userInfo:nil repeats:YES];
    }
    return _timer;
}

- (void)startAnimate
{
    if (self.dataModels.count == 0) return;
    [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:repeateInterval]];
}

- (void)stopAimate
{
    [self.timer setFireDate:[NSDate distantFuture]];
}

- (void)bannerAutoScroll
{
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.5 animations:^{
        CGPoint offset = weakSelf.scrollView.contentOffset;
        CGPoint newOffset = CGPointMake(offset.x + WY_Banner_Width, offset.y);
        weakSelf.scrollView.contentOffset = newOffset;
    } completion:^(BOOL finished) {
        CGFloat maxOffsetX = self.dataModels.count * WY_Banner_Width;
        CGPoint offset = self.scrollView.contentOffset;
        if (offset.x == maxOffsetX) {
            self.scrollView.contentOffset = CGPointZero;
        }
    }];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat maxOffsetX = self.dataModels.count * WY_Banner_Width;
    CGPoint offset = scrollView.contentOffset;
    if (offset.x >= maxOffsetX+1) {
        scrollView.contentOffset = CGPointZero;
    }else if (offset.x <= -1) {
        scrollView.contentOffset = CGPointMake(maxOffsetX, 0);
    }
    self.pageControl.currentPage = round(offset.x * 1.0 / WY_Banner_Width);
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopAimate];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self startAnimate];
}

#pragma mark - click
- (void)bannerPressed:(id)sender
{
    WYBannerModel *model = self.dataModels[self.pageControl.currentPage];
    if (model.actionUrl.length == 0) return;
    if ([self.delegate respondsToSelector:@selector(bannerViewDidSelect:actionUrl:)]) {
        [self.delegate bannerViewDidSelect:self actionUrl:model.actionUrl];
    }
}

@end
